package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.utils.MD5Utils;
import cn.itsource.basic.utils.StrUtils;
import cn.itsource.org.domain.Logininfo;
import cn.itsource.org.domain.User;
import cn.itsource.org.domain.dto.SmsCodeParam;
import cn.itsource.org.mapper.LogininfoMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.org.service.IUserService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhi
 * @since 2022-07-09
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void register(SmsCodeParam param) {
        //1. 参数非空校验
        if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getConfirmPassword())
        || StringUtils.isEmpty(param.getImageCode()) || StringUtils.isEmpty(param.getPhone())){
            throw new CustomException("数据异常");
        }
        //2. 两次密码是否输入一致
        if(!param.getPassword().equals(param.getConfirmPassword())){
            throw new CustomException("两次输入密码不一致");
        }
        //3. 校验用户是否存在
        User user = userMapper.loadByUserName(param.getPhone());
        if(user != null){
            throw new CustomException("用户已注册");
        }
        //4. 校验短信验证码是否存在
        Object smsCode = redisTemplate.opsForValue().get("register:" + param.getPhone());
        //1.  不存在：抛出自定义异常，短信验证码过期
        if (smsCode == null){
            throw new CustomException("验证码已过期，请重新发送短信");
        }
        String[] split = smsCode.toString().split(":");
        //2. 存在：校验是否正确
        if (!split[0].equals(param.getSmsCode())){
            throw new CustomException("验证码输入错误");
        }
        //5. 密码MD5加密，记录盐值
        String salt = StrUtils.getComplexRandomString(32);
        String md5password = MD5Utils.encrypByMd5(param.getPassword() + salt);
        //6. 先设置默认值，保存login_info
        User newUser = dtoToUser(param, salt, md5password);
        Logininfo logininfo = userToLogininfo(newUser);
        //7. 再保存user
        logininfoMapper.insert(logininfo);
        newUser.setLogininfoId(logininfo.getId());
        userMapper.insert(newUser);
    }

    public Logininfo userToLogininfo(User newUser){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(newUser,logininfo);
        logininfo.setDisable(1);
        logininfo.setType(1);
        return logininfo;
    }

    public User dtoToUser(SmsCodeParam param,String salt,String md5password){
        User user = new User();
        //用户名
        user.setUsername(param.getPhone());
        //账号
        user.setPhone(param.getPhone());
        //加密后的密码
        user.setPassword(md5password);
        //默认值
        user.setState(1);
        //盐值
        user.setSalt(salt);
        //创建账号时间
        user.setCreatetime(new Date());
        return user;
    }
}
